/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package remoting.exchange;


import common.constants.CommonConstants;
import common.exception.RemotingException;
import common.factory.MagicFactory;
import common.model.MagicUrl;
import remoting.ChannelHandlerAdapter;
import remoting.channel.ChannelHandler;
import remoting.exchange.support.ExchangeHandlerDispatcher;
import remoting.exchange.support.Replier;

/**
 * Exchanger facade. (API, Static, ThreadSafe)
 */
public class Exchangers {

    private Exchangers() {
    }

    public static ExchangeServer bind(String url, Replier<?> replier) throws RemotingException {
        return bind(MagicUrl.valueOf(url), replier);
    }

    public static ExchangeServer bind(MagicUrl url, Replier<?> replier) throws RemotingException {
        return bind(url, new ChannelHandlerAdapter(), replier);
    }

    public static ExchangeServer bind(String url, ChannelHandler handler, Replier<?> replier) throws RemotingException {
        return bind(MagicUrl.valueOf(url), handler, replier);
    }

    public static ExchangeServer bind(MagicUrl url, ChannelHandler handler, Replier<?> replier) throws RemotingException {
        return bind(url, new ExchangeHandlerDispatcher(replier, handler));
    }

    public static ExchangeServer bind(String url, ExchangeHandler handler) throws RemotingException {
        return bind(MagicUrl.valueOf(url), handler);
    }

    public static ExchangeServer bind(MagicUrl url, ExchangeHandler handler) throws RemotingException {
        if (url == null) {
            throw new IllegalArgumentException("url == null");
        }
        if (handler == null) {
            throw new IllegalArgumentException("handler == null");
        }
        url = url.addParameterIfAbsent(CommonConstants.CODEC_KEY, "exchange");
        return MagicFactory.getMagicFactory().getExchanger().bind(url, handler);
    }

    public static ExchangeClient connect(String url) throws RemotingException {
        return connect(MagicUrl.valueOf(url));
    }

    public static ExchangeClient connect(MagicUrl url) throws RemotingException {
        return connect(url, new ChannelHandlerAdapter(), null);
    }

    public static ExchangeClient connect(String url, Replier<?> replier) throws RemotingException {
        return connect(MagicUrl.valueOf(url), new ChannelHandlerAdapter(), replier);
    }

    public static ExchangeClient connect(MagicUrl url, Replier<?> replier) throws RemotingException {
        return connect(url, new ChannelHandlerAdapter(), replier);
    }

    public static ExchangeClient connect(String url, ChannelHandler handler, Replier<?> replier) throws RemotingException {
        return connect(MagicUrl.valueOf(url), handler, replier);
    }

    public static ExchangeClient connect(MagicUrl url, ChannelHandler handler, Replier<?> replier) throws RemotingException {
        return connect(url, new ExchangeHandlerDispatcher(replier, handler));
    }

    public static ExchangeClient connect(String url, ExchangeHandler handler) throws RemotingException {
        return connect(MagicUrl.valueOf(url), handler);
    }

    public static ExchangeClient connect(MagicUrl url, ExchangeHandler handler) throws RemotingException {
        if (url == null) {
            throw new IllegalArgumentException("url == null");
        }
        if (handler == null) {
            throw new IllegalArgumentException("handler == null");
        }
        url = url.addParameterIfAbsent(CommonConstants.CODEC_KEY, "exchange");
        return MagicFactory.getMagicFactory().getExchanger().connect(url, handler);
    }

}
